<?php if (!defined ( 'BASEPATH' )) exit ('No direct script access allowed');

/**
 * product model
 */
class M_product extends CI_Model {
    var $language_id;

    public function __construct() {
        parent::__construct ();
        $this->language_id = $this->session->userdata('language_id');
    }
    
    /**
     * 获取商品基础信息
     * @param  [int] $pid 商品id
     * @return array
     */
    public function get_product_info($pid){
        if(!is_numeric($pid)){
            return NULL;
        }
        
        $sql = 'select p.product_id, p.sku, p.price, p.image, p.max_buy_qty, p.min_buy_qty from products p where p.product_id = '.$pid;
        $result = $this->db->query($sql);
        if($result->num_rows() < 1){
            return NULL;
        }
        $product = $result->row_array();
        
        $sql = 'select pd.product_name, pd.product_short_name, pd.product_description from product_descriptions pd where pd.language_id = '.$this->session->userdata('language_id').' and pd.product_id = '.$pid;
        $result = $this->db->query($sql);
        if($result->num_rows() < 1){
            return NULL;
        }
        
        $product = array_merge($product, $result->row_array());
        return $product;
    }

    /**
     * 获得商品属性
     * @param  [int] $pid 商品id
     * @return [Array]
     */
    public function get_product_attributes($pid)
    {
        if(!is_numeric($pid)){
            return NULL;
        }

        $sql = "SELECT pa.product_id,
                       pa.option_id,
                        ( SELECT fd.description
                            FROM fnd_descriptions fd
                            WHERE fd.language_id = ?
                            AND fd.description_id = po.option_name_id
                        ) as option_name,
                      pa.option_value_id,
                      ( SELECT fd.description
                            FROM fnd_descriptions fd
                            WHERE fd.language_id = ?
                            AND fd.description_id = pov.option_value_name_id
                        ) as option_value_name,
                      pa.attribute_price,
                      pa.attribute_discount
                    FROM
                        product_attributes pa,
                        product_options po,
                        product_option_values pov
                    WHERE
                        pa.option_id = po.option_id
                    AND pa.option_value_id = pov.option_value_id
                    AND pa.product_id = ?";
        $results = $this->db->query($sql, array($this->language_id, $this->language_id, $pid));

        $options = [];
        if($results->num_rows() > 0){
            $query_results = $results->result_array();
            $flag = false;
            while (list(,$ov) = each($query_results)) {
                for ($i=0; $i < sizeof($options); $i++) { 
                    if(is_array($options[$i]) && in_array($ov['option_name'], $options[$i])){
                        array_push($options[$i]['option_values'], array(
                                                                    'option_value_id'   =>  $ov['option_value_id'],
                                                                    'option_value_name' =>  $ov['option_value_name']
                                                              ));
                        $flag = TRUE;
                        break;
                    }
                    $flag = FALSE;
                }

                if(!$flag){
                    array_push($options, array(
                                                'option_id'             =>  $ov['option_id'],
                                                'option_name'           =>  $ov['option_name'],
                                                'attribute_price'       =>  $ov['attribute_price'],
                                                'attribute_discount'    =>  $ov['attribute_price'],
                                                'option_values'         =>  array(array(
                                                                                'option_value_id'   =>  $ov['option_value_id'],
                                                                                'option_value_name' =>  $ov['option_value_name']
                                                                            ))
                                                ));
                    
                }
            }
        }
        return $options;
    }

    /**
     * [is_product_exists description]
     * @param  integer $pid [description]
     * @return boolean      [description]
     */
    public function is_product_exists($pid = -1)
    {
        $query = $this->db->get_where('products',array('product_id' => $pid));
        if ($query->num_rows() > 0) {
            return TRUE;
        }else{
            return FALSE;
        }
    }
}

/* End of file product.php */
/* Location: ./application/models/product.php */